package com.face.operate.jsu;

import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.tomcat.dbcp.dbcp2.PStmtKey;

import com.face.bean.jsu.LostPopulationMessage;
import com.face.bean.jsu.UsernameAndName;
import com.face.common.jsu.StringToDate;
import com.face.sql.jsu.JDBCUtil;
import com.face.sql.jsu.LostDao;
import com.face.sql.jsu.MessageDao;

public class LostMessage {

	private MessageDao dao = new MessageDao();

	/*
	 * 保存
	 */
	public void save(Map<String, String> map) {
		String gender = map.get("sex");
		int sex, age = 0;
		double stature = 0, weight = 0;
		if (!map.get("age").equals("")) {
			age = Integer.parseInt(map.get("age"));
		}
		if (!map.get("stature").equals("")) {
			stature = Double.parseDouble(map.get("stature"));
		}
		if (!map.get("weight").equals("")) {
			weight = Double.parseDouble(map.get("weight"));
		}
		if (gender.equals("男")) {
			sex = 1;
		} else if (gender.equals("女")) {
			sex = 0;
		} else {
			sex = 2;
		}
		Date date = StringToDate.StringToDate(map.get("time"));

		dao.save(map.get("username"), map.get("name"), sex, age, stature,
				weight, map.get("address"), map.get("location"), date,
				map.get("phone"), map.get("others"));

	}

	/*
	 * 根据username查询
	 */
	public List<LostPopulationMessage> selectByUsername(String username) {

		return dao.selectByUsername(username);
	}

	/*
	 * 根据username查询，批量
	 */
	public List<LostPopulationMessage> selectByUsername(Set<String> keySet) {

		return dao.selectByUsername(keySet);

	}

	/*
	 * 通过username以及name查询寻人启事(批量
	 */
	public List<LostPopulationMessage> selectByUsernameAndName(IdentityHashMap<String, String> usernames) {
		
		List<LostPopulationMessage> messages = new ArrayList<LostPopulationMessage>();
		
		for (Map.Entry<String, String> entry : usernames.entrySet()) {
			
			LostPopulationMessage message = new LostPopulationMessage();
			message = dao.selectByUsernameAndName(entry.getKey(),entry.getValue());
			messages.add(message);
			
		}
		return messages;
	}

	/*
	 * 根据username及name删除信息
	 */
	public void deleteByUsernameAndName(String username, String name) {

		dao.deleteByUsernameAndName(username, name);
	}

	public void updateById(String id, String name, String address, String age,
			String location, String others, String phone, String sex,
			String stature, String time,String weight) {
		int sex_tmp=2, age_tmp = 0;
		double stature_tmp = 0, weight_tmp = 0;
		if (!age.equals("")) {
			age_tmp = Integer.parseInt(age);
		}
		if (!stature.equals("")) {
			stature_tmp = Double.parseDouble(stature);
		}
		if (!weight.equals("")) {
			weight_tmp = Double.parseDouble(weight);
		}
		if (sex.equals("男")) {
			sex_tmp = 1;
		} else if (sex.equals("女")) {
			sex_tmp = 0;
		} else {
			sex_tmp = 2;
		}
		Date date = StringToDate.StringToDate(time);
		dao.updateById(Integer.parseInt(id),name,address,age_tmp,location,others,phone,sex_tmp,stature_tmp,date,weight_tmp);
	}

	//根据id值查找其username以及name值
	public UsernameAndName selectNameById(String id) {
		int  Id = Integer.parseInt(id);
		UsernameAndName name = dao.selectNameById(Id);
		return name;
	}

}
